//
//  MBDUserDetailCell.swift
//  MasterBoy
//
//  Created by kyson on 2023/10/23.
//

import Foundation
import YYText
import SnapKit
import Kingfisher


class MBDUserDetailCell: MBDCommonCell {

    private var titleLabel : YYLabel!
    private var modifyLabel : YYLabel!
    private var modifyImageView : UIImageView!

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.initialize()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        self.initialize()
    }
    
    override var cellModel: Any! {
        didSet {
            let cellModel = self.cellModel as! MBDUserDetailCellModel
            self.title = cellModel.itemName
            
            if (cellModel.detailText != nil && !cellModel.detailText!.isBlank) {
                self.detailType = .text
                self.detailTobeModify = cellModel.detailText
                
            } else if (nil != cellModel.detailImageURLString) {
                self.detailType = .image
                self.avatarTobeModify = cellModel.detailImageURLString
            } else {
                self.detailType = .text
                self.detailTobeModify = ""
            }
            
            self.accessable = false
            self.location = cellModel.location
            
        }
    }
    
    func initialize() {
        titleLabel = YYLabel()
        self.commonBackgroundView.addSubview(titleLabel)
        
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalTo(self.commonBackgroundView)
            make.left.equalTo(commonBackgroundView).offset(8)
        }
    }
    
    private func detailSet(){
        if .text == self.detailType {
            
            if(nil != modifyLabel && nil != modifyLabel.superview) {
                modifyLabel.removeFromSuperview()
            }
            modifyLabel = YYLabel()
            self.commonBackgroundView.addSubview(modifyLabel)
            
            modifyLabel.snp.makeConstraints { make in
                make.centerY.equalTo(self.commonBackgroundView)
                make.right.equalTo(self.rightArrowImage.snp.left).offset(-10)
            }
        } else {
            if(nil != modifyImageView &&  nil != modifyImageView.superview) {
                modifyImageView.removeFromSuperview()
            }
            modifyImageView = UIImageView()
            modifyImageView.layer.cornerRadius = 4
            modifyImageView.clipsToBounds = true
            self.commonBackgroundView.addSubview(modifyImageView)
            
            modifyImageView.snp.makeConstraints { make in
                make.centerY.equalTo(self.commonBackgroundView)
                make.right.equalTo(self.rightArrowImage.snp.left).offset(-10)
                make.size.equalTo(CGSizeMake(40, 40))
            }
        }
    }
    
    var detailType : MBDDetailType? {
        didSet {
            self.detailSet()
        }
    }
    
    var title : String? {
        didSet {
            
            let usernameAttributeString = NSMutableAttributedString(string: self.title ?? "")
            usernameAttributeString.yy_font = UIFont.systemFont(ofSize: 14)
            usernameAttributeString.yy_color = UIColor(colorHex: 0x333333)
            
            titleLabel.attributedText = usernameAttributeString
        }
        
        willSet {
        }
    }
    
    var detailTobeModify : String? {
        didSet {
            
            let modifyDetail = self.detailTobeModify ?? ""
            let usernameAttributeString = NSMutableAttributedString(string:  modifyDetail)
            usernameAttributeString.yy_font = UIFont.systemFont(ofSize: 14)
            usernameAttributeString.yy_color = UIColor(colorHex: 0x333333)
            
            modifyLabel.attributedText = usernameAttributeString
        }
    }
    
    var avatarTobeModify : String? {
        didSet {
            
            let avatar = self.avatarTobeModify ?? ""
            if avatar.hasPrefix(MBDPath2URLUtils.filePathPrefix) {
                let imageName = avatar.replacingOccurrences(of: MBDPath2URLUtils.filePathPrefix, with: "")
                
                modifyImageView.image = UIImage(named: imageName)
            } else {
                let url = URL.init(string: MBDPath2URLUtils.wrapAvatarURL(path: avatar))
                modifyImageView.kf.setImage(with: url)
            }
            
        }
    }
}
